package com.company.thread;


import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 保护性暂停线程模式测试
 */
@Slf4j
public class GuardedTest {


    private static final GuardedObject lock = new GuardedObject();
    private static Object ret;
    public static Object getRet() {
        return ret;
    }
    public static void main(String[] args) {

        //线程1获取线程2下载的内容
        new Thread(()->{
            System.out.println("handler获取downloader下载的内容...");
            Object response = lock.getResponse();
            ret = response;
            System.out.println(getRet());
        },"handler").start();


        //线程2下载内容
        new Thread(()->{
            System.out.println("downloader下载内容...");
            List<String> li = Downloader.downloadBaiduPageToStrs();

            lock.finishResponse(li);
        },"downloader").start();





    }



}

class Downloader{
    //模拟百度页面爬虫，存到lines里
    public static List<String> downloadBaiduPageToStrs(){
        List<String> lines = new ArrayList<>();
        lines.add("<html>");
        lines.add("<h1>");
        lines.add("大家好，我是百度");
        lines.add("</h1>");
        lines.add("</html>");
        return lines;
    }
}